//
//  XDUpdatePwdView.m
//  XDEducation
//
//  Created by apple on 2017/4/24.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDUpdatePwdView.h"
#import "XDUpdatePwdCell.h"
@interface XDUpdatePwdView ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property(nonatomic,strong) UITableView *tableView;

/** 页面数据源 */
@property(nonatomic,strong) NSArray *dataArr;

@end

static NSString *cellId = @"XDUpdatePwdCell";
static UIWindow *__sheetWindow = nil;

@implementation XDUpdatePwdView

{
    CGRect _frame;
    
    BOOL _isNeedMoneyPwd;   //诚信是金密码
    
    NSString *_oldpwd;
    NSString *_newpwd;
    NSString *_newpwdAgain;
}
-(instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
        
//        self.layer.masksToBounds = YES;
//        self.layer.cornerRadius = 5.f;
        
        _frame = frame;
        
        _tableView = [[UITableView alloc]init];
        _tableView.frame = frame;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self addSubview:_tableView];
        
        [_tableView registerNib:[UINib nibWithNibName:@"XDUpdatePwdCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:cellId];
//        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewClicked)];
        [self addGestureRecognizer:tap];
        
    }
    return self;
}

-(NSArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSArray arrayWithObjects:
                    @{@"icon":@"mine_pop_icon_old_default",@"title":@"旧  密  码:",@"placeholder":@"请输入旧密码"},
                    @{@"icon":@"mine_pop_icon_new_default",@"title":@"新  密  码:",@"placeholder":@"请输入新密码"},
                    @{@"icon":@"mine_pop_icon_new_default",@"title":@"新  密  码:",@"placeholder":@"请输入新密码"},
                @{@"icon":@"mine_pop_icon_ok_pressed",@"title":@"",@"placeholder":@"需要进入诚信是金的密码"},
                    nil];
    }
    return _dataArr;
}

#pragma mark - tableview  delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 50 ;
}
//header view
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, _frame.size.width, 20)];
    label.text = @"修改密码";
    label.font = [UIFont systemFontOfSize:18];
    label.textColor = UIColorFromRGB(0x4c4c4c);
    label.textAlignment = NSTextAlignmentCenter;
    [view addSubview:label];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50 ;
}

//footer view
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor whiteColor];
    view.userInteractionEnabled = YES;
    CGFloat buttonW = _frame.size.width/2;
    CGFloat buttonH = 50 ;
    CGFloat buttonX = 0;
    for (int i = 0; i < 2; i ++) {
        
        UIButton *button = [[UIButton alloc]init];
        button.frame = CGRectMake(buttonX + buttonW * i, 0, buttonW, buttonH);
        [button setTitleColor:UIColorFromRGB(0x4c4c4c) forState:UIControlStateNormal];
        if (i == 0) {
            [button setTitle:@"确定" forState:UIControlStateNormal];
        }else{
            [button setTitle:@"取消" forState:UIControlStateNormal];
        }
        button.tag = i;
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];
        
       
    }
    //背景线
    UIImageView *lineImageView = [[UIImageView alloc]init];
    lineImageView.image = [UIImage imageNamed:@"mine_pop_seg_vertical_default"];
    lineImageView.frame = CGRectMake(_frame.size.width/2, 0, 1, 50 );
    [view addSubview:lineImageView];
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50 ;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XDUpdatePwdCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    NSDictionary *dict = self.dataArr[indexPath.row];
    cell.icon.image = [UIImage imageNamed:dict[@"icon"]];
    cell.title.text = dict[@"title"];
    cell.textField.placeholder = dict[@"placeholder"];
    cell.textField.tag = indexPath.row;
    cell.textField.delegate = self;
    if(indexPath.row == 3 ){
        
        cell.textField.userInteractionEnabled = NO;
    }
    
    if(indexPath.row == 0){ //旧密码textfield
#warning 修改密码接口没用到旧密码 123456占位
        cell.textField.text = @"123456";
        _oldpwd = @"123456";
        cell.textField.userInteractionEnabled = NO;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    if(indexPath.row == 3){
//        
//        _isNeedMoneyPwd = !_isNeedMoneyPwd;
//        XDUpdatePwdCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//        
//        if (_isNeedMoneyPwd) {
//            
//             cell.icon.image = [UIImage imageNamed:@"mine_pop_icon_ok_pressed"];
//        }else{
//            
//            cell.icon.image = [UIImage imageNamed:@"mine_pop_icon_ok_default"];
//        }
//       
//        
//    }
}


-(void)show{

    UIWindow *window = [[UIWindow alloc] init];
    
    window.frame = CGRectMake(0.f, 0.f, [[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height);
    window.userInteractionEnabled = YES;
    window.backgroundColor = HUDBgColor;
    window.windowLevel = UIWindowLevelNormal;
    window.alpha = 1.f;
    window.hidden = NO;
    
    [window addSubview:self];
    __sheetWindow = window;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hide)];
    [window addGestureRecognizer:tap];
}


-(void)hide{

    
    [UIView animateWithDuration:0.5f animations:^{
        
        self.alpha = 0.f;
        
    } completion:^(BOOL finished) {
        
        __sheetWindow.hidden = YES;
        
        __sheetWindow = nil;
        
        [self removeFromSuperview];
       
    }];
}


/**
 确定取消按钮
 */
-(void)buttonClicked:(UIButton *)button{
    
    if (button.tag == 0) {  //确定按钮
        
        if (self.sureUpdatePwdBlock) {
            self.sureUpdatePwdBlock(_oldpwd,_newpwd,_newpwdAgain);
        }
        
        
    }else{      //取消按钮
    
        [self hide];
    }

}


-(void)viewClicked{
    [self endEditing:YES];
}


-(void)textFieldDidEndEditing:(UITextField *)textField{

    switch (textField.tag) {
        case 0:
            //_oldpwd = textField.text;
            break;
        case 1:
            _newpwd = textField.text;
            break;
        case 2:
            _newpwdAgain = textField.text;
            break;
            
        default:
            break;
    }
}


@end
